// pages/order/order.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollViewHeigth: 300,
    tabSelected: 0,
    discount:100,

    pages: 0,
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    options.pageName && (wx.setNavigationBarTitle({
      title: options.pageName,
    }))
    this.setData({
      tabSelected:options.otype || 0,
      discount: app.globalData.userInfo.discount ? parseFloat(app.globalData.userInfo.discount) : 100
    })
    var that = this;
    var query = wx.createSelectorQuery();
    //选择id
    query.select('#bar').boundingClientRect();
    query.exec(function(res) {
      console.log(res[0].height)
      console.log(wx.getSystemInfoSync())
      that.setData({
        marginTop: res[0].height +10,
        scrollViewHeigth: wx.getSystemInfoSync().windowHeight - res[0].height+46
      })
    })
  },

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      tabSelected: e.detail.current,
      pages: 0,
      orderList: []
    });
    this.getOrderList();
  },

  //点击切换
  clickTab: function(e) {
    if (e.currentTarget.dataset.idx != this.data.tabSelected) {
      this.setData({
        tabSelected: e.currentTarget.dataset.idx,
        pages: 0,
        orderList: []
      });
    }
  },

  getOrderList: function () {
    var that = this;
    this.data.pages++;
    var data = {
      point_url: app.server.agentApi.memberOrder,
      openid:app.globalData.openid,
      type: parseInt(this.data.tabSelected)+1,
      pageNumber: this.data.pages,
      pageSize: 10,
      sortName:'created_at',
      sortOrder:'desc'
    };
    app.server.getConnect(data).then(function (res) {
      console.log(res);
      that.setData({
        orderList: that.data.orderList.concat(res.data.rows)
      })
    })
  },

  // 跳转订单详情
  toOrderInfo: function(e) {
    wx.navigateTo({
      url: '../orderInfo/orderInfo?id=' + e.currentTarget.dataset.id,
    })
  },

  // 选择付款方式
  choicePayWay:function(e){
    var that = this;
    app.showConfirm('付款方式', '', true, function () {
      that.setData({
        pay_type: 0
      })
      that.toPay(e.currentTarget.dataset.id);
    }, function () {
      that.setData({
        pay_type: 1
      })
      that.toPay(e.currentTarget.dataset.id);
    }, '余额', '赠送金')
  },

  // 支付
  toPay:function(id){
    var that = this;
    var data = {
      point_url: app.server.agentApi.orderAction,
      openid: app.globalData.openid,
      id: id,
      pay_type: this.data.pay_type,
      action:'pay',
      remarks: ''
    };
    app.server.getConnect(data).then(function (res) {
      console.log(res);
      wx.showToast({
        title: '支付成功',
      })
      that.setData({
        tabSelected :1,
        pages: 0,
        orderList: []
      })
      that.getOrderList();
    })
  },

  // 取消订单
  toCancel: function (e) {
    var that = this;
    app.showConfirm('提醒','是否确定取消订单',true,function(){
      var data = {
        point_url: app.server.agentApi.orderAction,
        openid: app.globalData.openid,
        id: e.currentTarget.dataset.id,
        pay_type: that.data.pay_type,
        action: 'cancel'
      };
      app.server.getConnect(data).then(function (res) {
        console.log(res);
        that.setData({
          pages: 0,
          orderList: []
        })
        that.getOrderList();
      })
    })
  },

  // 申请退款
  toRefund: function (e) {
    var that = this;
    var data = {
      point_url: app.server.agentApi.orderAction,
      openid: app.globalData.openid,
      id: e.currentTarget.dataset.id,
      action: 'refund'
    };
    app.server.getConnect(data).then(function (res) {
      console.log(res);
      that.setData({
        tabSelected: 1,
        pages: 0,
        orderList: []
      })
      that.getOrderList();
      wx.showToast({
        title: '申请成功',
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if(this.data.tabSelected==0){
      this.setData({
        pages: 0,
        orderList: []
      })
      this.getOrderList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // this.getOrderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
